﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace SongSharp.Configuration
{
    public static class SettingsManager
    {
        public const string SETTINGS_FILE = "settings.xml";

        private static Settings ms_settings;
        public static Settings DefaultSettings
        {
            get
            {
                if (ms_settings == null)
                {
                    ms_settings = Load();
                }
                return ms_settings;
            }

            private set
            {
                ms_settings = value;
            }
        }

        public static Settings Load()
        {
            if (File.Exists(SETTINGS_FILE))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Settings));
                ms_settings = xmlSerializer.Deserialize(new FileStream(SETTINGS_FILE, FileMode.Open)) as Settings;
            }
            else
            {
                ms_settings = Settings.CreateDefaultSettings();
            }
            return ms_settings;
        }

        public static void Save()
        {
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Settings));
                xmlSerializer.Serialize(new FileStream(SETTINGS_FILE, FileMode.Create), ms_settings);
            }
            catch (IOException) { }
        }
    }
}
